<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field['options'].displayStyle"
  >
    <div class="scene-waiting-dual-share" :style="getStyle('area')">
      <div class="flex-box scene-waiting-inside">
        <div class="item flex-box-left">
          <template v-for="item in waitingPatientInfos">
            <div class="queue-item">
              <div class="queue-name" :data-info="item[optionsInfo['registerObjNameKey']] || item.treatmentRoomName ">
                <ScrollX>
                  {{ item[optionsInfo['registerObjNameKey']] || item.treatmentRoomName }}
                </ScrollX>
              </div>
              <div class="patient-name" v-html="getCurrentPatient(item) || ''"></div>
            </div>
          </template>
        </div>
        <div class="item flex-box-right">
          <div class="header-area">候诊患者({{getWaitingPatientNumber}})</div>
          <div class="list-area">

            <template v-for="(patient) in waitingPatientSummaryInfos">
              <div class="group">
                <template v-for="(item) in patient">
                  <div class="group-item" :data-info="patient.queuingId">
                    <div class="child-inside">
                      {{ item?.patientName || '\u00a0' }}
                    </div>
                  </div>
                </template>
              </div>
            </template>
          </div>
        </div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import {pxToVw} from '@/utils/common';
import emitter from "@/utils/designer/emitter";
import {buildParentApi} from "@/utils/designer/util";
import ScrollX from '@c/public/seamlessScroll/ScrollX.vue'
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";

let buildParent = buildParentApi();
export default {
  name: "scene-waiting-dual-share-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    CompOperate,
    ScrollX
  },

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    devType: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },

    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
  },

  data() {
    return {
      week: '',
      mockOpen: false,
      total: 0,
      optionsInfo: Object(),
      waitPatientList: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
    }
  },

  computed: {

    // 设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        switch (val) {
          case 'area':
            Reflect.set(Obj, 'height', pxToVw(info.areaHeight));
            return Obj;
          case 'head':
            Reflect.set(Obj, 'height', pxToVw(info.headHeight));
            Reflect.set(Obj, 'color', `${(info.headerFontColor)}`);
            Reflect.set(Obj, 'background', `${(info.headerBackgroundColor)}`);
            return Obj;
        }
      }
    },

    // 当前就诊的患者|| 医生暂停接诊
    getCurrentPatient() {
      return function (val) {
        /**
         * 在线状态 0在线 1暂停 2退出
         **/
        const self = this;
        let {patientOrderNo} = (self.optionsInfo) || Array();
        let type = parseInt(val.onlineStatus);
        switch (type) {
          case 0:
          case 2:
            if (!!(val.treatingPatient) && !!(val.treatingPatient.length)) {
              let currentPatient = Array();
              (val.treatingPatient).forEach((item) => {
                let patientOrderNoInfo = `<span class="${patientOrderNo ? 'patient-orderNo' : 'patient-orderNo2'}">${(item.orderNo) || '\u00a0'}</span>`;
                let temp = `${patientOrderNoInfo} ${(item.patientName) || '\u00a0'}`
                currentPatient.push(temp)
              })
              return (this.$lodash.join(currentPatient, ' '));
            } else {
              return '\u00a0'
            }
          case 1:
            return '暂停接诊';
        }
      }
    },

    // 获取等待的患者数量
    getWaitingPatientNumber(){
      const self = this;
      let devType = (this.devType);
      let showRowNums = 2
      let WaitingPatientList = this.$store.getters['getWaitingPatientInfo'];
      let list = Array();
      if (devType) {
        list = (WaitingPatientList.apiData);
      } else {
        list = (WaitingPatientList.mockData);
      }
      let summaryList = Array();
      if (list.length > showRowNums) {
        summaryList = list.slice(0, showRowNums);
      } else {
        let arr = (new Array(showRowNums - list.length).fill({}));
        summaryList = [...list, ...arr];
      }
      let summary = this.$lodash.map(summaryList, "waitPatientList");
      let flattenArr = this.$lodash.flattenDeep(summary);
      let uniqByArr = this.$lodash.uniqBy(flattenArr, 'queuingId');
      const filteredArray = self.$lodash.filter(uniqByArr, function(obj) {
        return !self.$lodash.isEmpty(obj);
      });
      return filteredArray.length || 0
    },

    // 候诊队列数据
    waitingPatientInfos() {
      let devType = (this.devType);
      let WaitingPatientList = this.$store.getters['getWaitingPatientInfo'];
      if (devType) {
        if (WaitingPatientList.apiData.length > 2) {
          return WaitingPatientList.apiData.slice(0, 2);
        } else {
          let arr = (new Array(2 - WaitingPatientList.apiData.length).fill({}));
          return [...WaitingPatientList.apiData, ...arr];
        }
      } else {
        if (WaitingPatientList.mockData.length > 2) {
          return WaitingPatientList.mockData.slice(0, 2);
        } else {
          let arr = (new Array(2 - WaitingPatientList.mockData.length).fill({}));
          return [...WaitingPatientList.mockData, ...arr];
        }
      }
    },

    //等待就这的患者
    waitingPatientSummaryInfos() {
      let devType = (this.devType);
      let showRowNums = 2
      let WaitingPatientList = this.$store.getters['getWaitingPatientInfo'];
      let list = Array();
      if (devType) {
        list = (WaitingPatientList.apiData);
      } else {
        list = (WaitingPatientList.mockData);
      }
      let summaryList = Array();
      if (list.length > showRowNums) {
        summaryList = list.slice(0, showRowNums);
      } else {
        let arr = (new Array(showRowNums - list.length).fill({}));
        summaryList = [...list, ...arr];
      }
      let summary = this.$lodash.map(summaryList, "waitPatientList");
      let flattenArr = this.$lodash.flattenDeep(summary);
      let uniqByArr = this.$lodash.uniqBy(flattenArr, 'queuingId');
      if (uniqByArr.length > 18) {
        let flattenArr2 = uniqByArr.slice(0, 18);
        return this.$lodash.chunk(flattenArr2, 3)
      } else {
        let arr = (new Array(18 - uniqByArr.length).fill({}));
        let flattenArr2 = [...uniqByArr, ...arr];
        return this.$lodash.chunk(flattenArr2, 3)
      }
    },

  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      switch (key) {
        case "patientListInfo":
          let patientList = this.$lodash.cloneDeep(data);
          buildParent.executeParentMethod('writeLog', JSON.stringify({sign: '模板-患者信息', key: key, data: data}));
          this.$store.dispatch('setWaitingPatientInfo', patientList)
          break
      }
    }
  },

  watch: {
    field: {
      handler: function (val) {
        let key = (val.options.config);
        this.optionsInfo = (val.options[key]);
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },
};
</script>

<style lang="scss" scoped>
.scene-waiting-dual-share {
  background-color: transparent;
  color: #333;
  height: 100%;
  display: flex;
  justify-content: center;
  flex-direction: column;
  padding: 14px 20px;
  box-sizing: border-box;

  .scene-waiting-inside {
    width: 100%;

    .flex-box-left {
      margin-right: 10px;
      display: flex;
      min-width: calc(36% - 12px);
      border-radius: 0;
      flex: 0;
      box-sizing: border-box;
      background: transparent;
      flex-direction: column;

      .queue-item {
        flex: 1;
        border-radius: 12px;
        overflow: hidden;
        display: flex;
        flex-direction: column;
        background: rgba(0, 89, 179, 1);
        color: #EBEEF5;

        .queue-name {
          display: flex;
          flex-grow: 1;
          min-height: 50%;
          width: 100%;
          justify-content: center;
          align-items: center;
          align-self: center;
          font-size: 100px;
          background: rgba(0, 71, 143, 1);
          white-space: nowrap;
        }

        .patient-name {
          display: flex;
          flex-grow: 1;
          min-height: 50%;
          width: 100%;
          font-size: 88px;
          justify-content: center;
          align-items: center;
          align-self: center;
          white-space: nowrap;
        }

        &:first-child {
          margin-bottom: 10px;
        }

        &:last-child {
          margin-top: 10px;
        }
      }
    }

    .flex-box-right {
      border-radius: 12px;
      overflow: hidden;
      margin-left: 10px;
      box-sizing: border-box;
      flex: 1;
      min-width: calc(100% - 36% - 12px);
      display: flex;
      flex-direction: column;

      .header-area {
        width: 100%;
        font-size: 60px;
        display: flex;
        justify-content: center;
        text-align: center;
        height: 120px;
        align-self: center;
        align-items: center;
        color: #0059B3;
        background: linear-gradient(180deg, #FFFFFF 0%, #F8F8F8 100%);
      }

      .list-area {
        display: flex;
        flex-direction: column;
        flex-grow: 1;
        width: 100%;
        height: 100%;

        .group {
          display: flex;
          flex-grow: 1;
          flex-direction: row;

          &:nth-child(even) {
            background-color: #F8F8F8;
          }

          .group-item {
            display: flex;
            justify-content: center;
            text-align: center;
            align-items: center;
            align-self: center;
            flex-grow: 1;
            height: 100%;
            min-width: calc(100% / 3);
            color: #333333;
            font-size: 50px;
            white-space: nowrap;
            overflow: hidden;
            .child-inside{
              padding: 0 24px;
              @include singleEllipsis(100%)
            }
          }
        }
      }
    }
  }
}
</style>
